import { message } from "antd";
import axios from "axios";
import {getToken} from '../utils/user-token'
const instance = axios.create({
    timeout: 10 * 1000
})
export default instance

//request 拦截：每次请求带上token
instance.interceptors.request.use(
    config=>{
        config.headers['Authorization']=`Bearer ${getToken()}` //JWT固定格式
        return config
    },
    error=>Promise.reject(error)
    
)


instance.interceptors.response.use(
    res => {
        const resData = (res.data || {}) as ResDataType
        const {errno,data,msg}=resData
        if(errno!==0){
            if(msg){
                message.error(msg)
            }
        }
        return data as any
    }

)

export type ResType = {
    errno: number
    data?: ResDataType
    msg?: string
}
export type ResDataType = {
    [key: string]: any

}